package wxdb.ui;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import java.awt.*;
import java.util.*;

public class QueryResult extends JDialog{
	
	private JPanel pane = new JPanel();
	private JTable resultTable = new JTable();
	HashMap<String,ArrayList> result;

	void buildConstraints(GridBagConstraints gbc,int gx, int gy, int gw, int gh, int wx, int wy){
		gbc.gridx = gx;			
		gbc.gridy = gy;			
		gbc.gridwidth = gw;		
		gbc.gridheight = gh;	
		gbc.weightx = wx;		
		gbc.weighty = wy;		
	}
	
	
	private void setMyPane(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);
		
		JScrollPane tablePane = new JScrollPane(resultTable);
		tablePane.setBounds(0, 0, 600, 600);
		pane.add(tablePane);

		setLocation(200,80);
		setContentPane(pane);
	}
	
	private int totoalRows = 10;;  
	private int totalColumns = 10;
	private String[][] myObjs = new String[totoalRows][totalColumns];
	
	void createTable(){
		try{
			
			Set<String> keyset = result.keySet();
			final ArrayList<String> keys = new ArrayList<String>();
			keys.addAll(keyset);
			
			totalColumns = keyset.size();
			for(String key : keyset){
				totoalRows = result.get(key).size();
				break;
			}
			
			myObjs = new String[totoalRows][totalColumns];
			
			TableModel dataModel = new AbstractTableModel(){   
				public int getColumnCount(){
					return totalColumns;   
				}
				public int getRowCount(){   
					return myObjs != null ? myObjs.length : 0;   
				}
				public String getColumnName(int columnIndex){
					return keys.get(columnIndex);   
				}   
				public Object getValueAt(int row, int col){
					String key = keys.get(col);
					myObjs[row][col] = result.get(key).get(row).toString(); 		
					return myObjs[row][col];
				}   
			};
			this.resultTable.setModel(dataModel);   
		}catch(Exception   ex)   {   
			ex.printStackTrace();
		}   
	}
	
	
	public QueryResult(HashMap<String,ArrayList> hm){
		result = hm;
		createTable();
		setTitle("QueryResult");
		setSize(600,600);
		setMyPane();
		setVisible(true);
		setResizable(false);
	}
}
